package com.yphone.cjencrypt;

import android.util.Base64;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;


public class EncryptUtils {
    private static final byte[] CloudHookDESIV = {85, 60, 5, 26, 121, 68, 19, 73};
    private static final byte[] CloudHookDESkey = {66, 28, 59, 34, 83, 16, 116, 12};
    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final String[] KeyArray = {"3949729b-9a58-4ffc-b720-770f67212d1c", "2d6aa33c-9638-42ab-87fc-78ae6d8f923c", "9628a2e0-9bda-4c0b-b2fd-902063900474", "0b2ce052-5930-42bc-a2d2-7e0e20aeeb35", "26c0da7d-9fe5-4236-8936-f0229dc63dc4", "db36880a-a810-46eb-9c73-603f1e6ce686", "626dfc8e-7d53-4532-a85d-6ef09882a49d", "c4de589e-a409-46fe-bbee-60e0d3e5d7dd"};
    static AlgorithmParameterSpec iv = null;
    private static Key key = null;

    public static String encode(String data) throws Exception {
        return encode(data, CloudHookDESkey, CloudHookDESIV);
    }

    public static String encode(String data, byte[] DESkey, byte[] DESIV) throws Exception {
        init(DESkey, DESIV);
        Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        enCipher.init(1, key, iv);
        return Base64.encodeToString(enCipher.doFinal(data.getBytes("utf-8")), 2);
    }

    private static void init(byte[] DESkey, byte[] DESIV) {
        try {
            DESKeySpec keySpec = new DESKeySpec(DESkey);
            iv = new IvParameterSpec(DESIV);
            key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e2) {
            e2.printStackTrace();
        } catch (InvalidKeySpecException e3) {
            e3.printStackTrace();
        }
    }

    public static String decode(String data) throws Exception {
        return decode(data, CloudHookDESkey, CloudHookDESIV);
    }

    public static String decode(String data, byte[] DESkey, byte[] DESIV) throws Exception {
        init(DESkey, DESIV);
        Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        deCipher.init(2, key, iv);
        return new String(deCipher.doFinal(Base64.decode(data, 0)), "UTF-8");
    }

    public static String sign(String data, int index) {
        if (index < 0 || index >= KeyArray.length) {
            return "";
        }
        return encryptMD5ToString(data + KeyArray[index]);
    }

    public static String encryptMD5ToString(String data) {
        if (data == null || data.length() == 0) {
            return "";
        }
        return encryptMD5ToString(data.getBytes());
    }

    public static String encryptMD5ToString(byte[] data) {
        return bytes2HexString(encryptMD5(data));
    }

    public static byte[] encryptMD5(byte[] data) {
        return hashTemplate(data, "MD5");
    }

    private static byte[] hashTemplate(byte[] data, String algorithm) {
        byte[] bArr = null;
        if (data == null || data.length <= 0) {
            return bArr;
        }
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(data);
            return md.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return bArr;
        }
    }

    private static String bytes2HexString(byte[] bytes) {
        if (bytes == null) {
            return "";
        }
        int len = bytes.length;
        if (len <= 0) {
            return "";
        }
        char[] ret = new char[(len << 1)];
        int j = 0;
        for (int i = 0; i < len; i++) {
            int j2 = j + 1;
            ret[j] = HEX_DIGITS[(bytes[i] >> 4) & 15];
            j = j2 + 1;
            ret[j2] = HEX_DIGITS[bytes[i] & 15];
        }
        return new String(ret);
    }
}